package third.lib.zfb.outpay.trans;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import third.lib.common.exception.ThirdException;
import third.lib.common.logger.ThirdLogger;
import third.lib.common.status.ThirdStatus;
import third.lib.zfb.common.config.ZfbAppConfig;
import third.lib.zfb.common.model.PayInfo;
import third.lib.zfb.common.utils.ZfbUtils;
import third.lib.zfb.outpay.trans.model.NotifyCommon;
import third.lib.zfb.outpay.trans.model.TransBizContent;
import third.lib.zfb.outpay.trans.model.TransOrderChangedNotify;
import third.lib.zfb.outpay.trans.model.TransRet;
import third.lib.zfb.outpay.trans.type.NotifyInterface;
import third.lib.zfb.outpay.trans.type.TransType;

@Service
public class ZfbTrans {
    private final Logger logger = LoggerFactory.getLogger(ZfbTrans.class);
    @Autowired
    ThirdLogger thirdLogger;
    @Autowired
    ZfbAppConfig config;

    public ZfbTrans() {
    }

    public TransRet transBToUser(String bizNo, BigDecimal amount, String title, String userId, TransType type) throws ThirdException {
        TransBizContent content = new TransBizContent();
        content.setOut_biz_no(bizNo);
        content.setTrans_amount(amount);
        content.setOrder_title(title);
        content.setProduct_code(type.getType());
        content.setBiz_scene("DIRECT_TRANSFER");
        PayInfo payInfo = new PayInfo();
        payInfo.setIdentity_type("ALIPAY_USER_ID");
        payInfo.setIdentity(userId);
        content.setPayee_info(payInfo);

        AlipayFundTransUniTransferResponse response;
        try {
            AlipayClient alipayClient = ZfbUtils.getRequest(this.config.getAppId(), this.config.getPriKey(), this.config.getPubAppCrtPath(), this.config.getPubAliCrtPath(), this.config.getPubRootCrtPath());
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
            request.setBizContent(JSON.toJSONString(content));
            response = (AlipayFundTransUniTransferResponse)alipayClient.certificateExecute(request);
        } catch (ThirdException var11) {
            throw var11;
        } catch (AlipayApiException var12) {
            var12.printStackTrace();
            throw new ThirdException(ThirdStatus.ZFB);
        }

        if (!response.isSuccess()) {
            this.logger.error(JSON.toJSONString(response));
            throw new ThirdException(ThirdStatus.ZFB);
        } else {
            TransRet transRet = new TransRet();
            transRet.setBizNo(response.getOutBizNo());
            transRet.setOrderId(response.getOrderId());
            transRet.setPayFundOrderId(response.getPayFundOrderId());
            transRet.setStatus(response.getStatus());
            transRet.setTransDate(response.getTransDate());
            return transRet;
        }
    }

    public NotifyCommon zfbStatusChange(HttpServletRequest request) throws ThirdException {
        Map requestParams = request.getParameterMap();
        Map<String, String> params = new HashMap();
        Iterator iter = requestParams.keySet().iterator();

        String name;
        String msg;
        while(iter.hasNext()) {
            name = (String)iter.next();
            String[] values = (String[])((String[])requestParams.get(name));
            msg = "";

            for(int i = 0; i < values.length; ++i) {
                msg = i == values.length - 1 ? msg + values[i] : msg + values[i] + ",";
            }

            params.put(name, msg);
        }

        thirdLogger.info(JSON.toJSONString(params));
        String charset = (String)params.get("charset");
        name = (String)params.get("sign_type");
        boolean bResult = false;

        try {
            bResult = AlipaySignature.rsaCertCheckV1(params, this.config.getPubAliCrtPath(), charset, name);
        } catch (AlipayApiException var9) {
            var9.printStackTrace();
            throw new ThirdException(ThirdStatus.NET);
        }

        if (!bResult) {
            throw new ThirdException(ThirdStatus.NET);
        } else {
            msg = (String)params.get("msg_method");
            if (msg.equals(NotifyInterface.TransOrderChange.getName())) {
                TransOrderChangedNotify notify = new TransOrderChangedNotify();
                notify.setActionType((String)params.get("action_type"));
                notify.setBizScene((String)params.get("biz_scene"));
                notify.setOrderId((String)params.get("order_id"));
                notify.setOriginInterface("origin_interface");
                notify.setOutBizNo((String)params.get("out_biz_no"));
                notify.setPayDate((String)params.get("pay_date"));
                notify.setPayFundOrderId((String)params.get("pay_fund_order_id"));
                notify.setProductCode((String)params.get("product_code"));
                notify.setStatus((String)params.get("status"));
                notify.setTransAmount((String)params.get("trans_amount"));
                notify.setMethod((String)params.get("method"));
                notify.setNotifyId((String)params.get("notify_id"));
                return notify;
            } else {
                throw new ThirdException(ThirdStatus.ZFB);
            }
        }
    }
}

